package com.honghu.villagepost.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.JsResult;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;

import com.honghu.villagepost.R;
import com.honghu.villagepost.base.BaseActivity;
import com.honghu.villagepost.util.WebViewOB;

import butterknife.BindView;

import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;

/**
 * @作者 cjh
 * @日期 2017/11/18 17:40
 * @描述 webview
 */

public class WebViewActivity extends BaseActivity {

    /**
     * 标题名称
     */
    public static final String REQUEST_NAME_TITLE = "title";
    /**
     * url参数名
     */
    public static final String REQUEST_NAME_URL = "url";

    @BindView(R.id.ll_web_root)
    LinearLayout mLlWebRoot;

    @BindView(R.id.webView)
    WebView mWebView;

    private String mUrl;

    private String mTitle;

    protected void initView() {
        WebSettings settings = mWebView.getSettings();
        settings.setJavaScriptEnabled(true);
        mWebView.getSettings().setDomStorageEnabled(true);
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.startsWith("http:") || url.startsWith("https:")) {
                    view.loadUrl(url);
                } else {
                    try {
                        Intent intent = new Intent();
                        intent.setAction(Intent.ACTION_VIEW);
                        intent.setData(Uri.parse(url));
                        startActivity(intent);
                    } catch (Exception e) {
                    }
                }
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }
        });

        // 设置允许加载混合内容
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mWebView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }

        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                return super.onJsAlert(view, url, message, result);
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                if (title.length() > 17) title = title.substring(0, 17) + "...";
                if (!TextUtils.isEmpty(mTitle)) {
                    setMainTitle(mTitle);
                } else {
                    setMainTitle(title);
                }
            }
        });
        setMainTitle(mTitle);
        mWebView.loadUrl(mUrl);
        mWebView.addJavascriptInterface(new WebViewOB(this), "android");
        mWebView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()) {  //表示按返回键
                        mWebView.goBack();   //后退
                        return true;    //已处理
                    }
                }
                return false;
            }
        });
    }


    public static void start(Context context, String title, String url) {
        Intent intent = new Intent(context, WebViewActivity.class);
        intent.putExtra(WebViewActivity.REQUEST_NAME_TITLE, title);
        intent.putExtra(WebViewActivity.REQUEST_NAME_URL, url);
        intent.setFlags(FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_webview;
    }

    @Override
    protected void init(Bundle savedInstanceState, View container) {
        Bundle bundle = getIntent().getExtras();
        mUrl = bundle.getString(REQUEST_NAME_URL);
        mTitle = bundle.getString(REQUEST_NAME_TITLE);

        initView();
    }
}
